package com.hjh.java.seckill.jredis.lock;

import com.hjh.java.seckill.framework.RedisClient;
import com.hjh.java.seckill.framework.RedisFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import sun.reflect.generics.tree.ReturnType;

import java.io.Serializable;
import java.util.Collections;
import java.util.Random;

public class RedisLock {

    private String  setNx_and_expire= "if redis.call('setnx',KEYS[1],KEYS[2] ==1 THEN \n" +
            "return redis.call('expire',KEYS[1],KEYS[3];\n"+ "end \n"+ "return nil;";
    JedisPool jedisPool = null;


    private boolean lock(Object key,int expire,int timeout){
        long nano = System.nanoTime();
        try {
            do{
                if(setnxAndExpire22(String.valueOf(key),timeout)){
                    return true;
                }

                Thread.sleep(5, new Random().nextInt(500));
            }while (System.nanoTime()-nano < expire*1000*1000000L);

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    private boolean setnxAndExpire22(String key, int timeout) {


//        RedisClient defaultClient = RedisFactory.getDefaultClient();
//
//        JedisPool jedisPool = defaultClient.jedisPool;
//        Jedis client = jedisPool.getResource();
//
//
//        try{
//            client.eval("".getBytes(), ReturnType.VALUE,3, Serializable(key))
//
//        }

        return false;
    }
}
